package com.liyuan.yiqixing.controller;

import com.alibaba.fastjson.JSONObject;
import com.liyuan.yiqixing.Utils;
import com.liyuan.yiqixing.model.ResultJson;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class AuthenticationController {
    @Autowired
    Utils utils;
    @Autowired
    AuthenticationService authenticationService;
    @Autowired(required = false)
    JavaMailSender javaMailSender;//可以正常使用但就是显示这报错！！！
    //    @Bean
//    public JavaMailSender javaMailSender(){
//        return new JavaMailSenderImpl();
//    }
    @Value("${spring.mail.username}")
    private String fromMail;

    @GetMapping("/register")
    public void register(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/register.html").forward(request, response);
    }

    @GetMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/login.html").forward(request, response);
    }

    @PostMapping("/register")
    public void register(@RequestParam("nickname") String nickname,
                         @RequestParam("password") String password,
                         @RequestParam("role") Integer role,//1:义工；2:店家
                         @RequestParam("account") String account,//邮箱地址
                         @RequestParam("VRCode")String VRCode,//邮箱验证码
                         HttpServletResponse response,
                         HttpServletRequest request) throws IOException {
        User user = authenticationService.retrieveUser(account);
        if(!user.getActiveCode().trim().equals(VRCode.trim())){
            //验证失败时！！！ TODO
            return;
//            response.sendRedirect();
        }
        user.setNickname(nickname);
        user.setPassword(password);
        user.setRole(role);
        authenticationService.insertUser(user);
        response.sendRedirect("/index.html");
    }

    @PostMapping("/retrieveVRCode")
    public ResultJson retrieveVRCode(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String toMail = (String) jsonObject.get("email");
        User user_ = authenticationService.retrieveUser(toMail);
        if(user_!=null){
            return new ResultJson(-1,"该邮箱已经注册过了！！",null);
        }
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(fromMail);
        simpleMailMessage.setTo(toMail);
        simpleMailMessage.setSubject("义启行验证码");
        String VRCode = utils.retrieveVRCode();
        simpleMailMessage.setText("验证码：" + VRCode);
        javaMailSender.send(simpleMailMessage);
        User user = new User();
        user.setActiveCode(VRCode);
        user.setAccount(toMail);
        authenticationService.insertPreUser(user);
        return new ResultJson(200, "发送验证码成功！！", null);
    }
    @PostMapping("/authentication_")
    public ResultJson<Object> authenticate_(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal.equals("anonymousUser")){
            return new ResultJson<>(-1,"未登录！",null);
        }
        User user = (User) principal;
        user.setPassword(null);
        user.setAccount(null);
        return new ResultJson<>(200,"get user success",user);
    }
}
